﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Evoucher.Business.Models;
using Mani.Common;

public partial class admin_vouchers_transactions : System.Web.UI.Page
{
    /// <summary>
    /// Reporting Page for Transactions on Management level.
    /// Following fileds should be searchable:
    /// TransactionDate, EmailAddress, Username, VoucherType, PackageId, PaymentGateway, Voucher Serial Number
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            GetTransactions(1, true);
    }

    private void GetTransactions(int pageNumber, bool setPageCount)
    {
        IVoucherRepository repo = new VoucherRepository();
        if (setPageCount)
            gvTransactions.VirtualItemCount = repo.GetCountTransactions();

        List<Transaction> transactions = repo.GetTransactions(gvTransactions.PageSize, pageNumber);
        for (int i = 0; i < transactions.Count; i++)
        {
            transactions[i].TransactionStateReference.Load();
            transactions[i].PaymentGatewayReference.Load();
            transactions[i].aspnet_UsersReference.Load();
        }

        gvTransactions.DataSource = transactions;
        gvTransactions.DataBind();
    }

    public string GetUserName(object user)
    {
        if ( user == null )
            return String.Empty;

        return ((aspnet_Users)user).UserName;
    }

    protected void gvTransactions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTransactions.PageIndex = e.NewPageIndex;
        GetTransactions(e.NewPageIndex + 1, false);
    }
}